# -*- encoding: utf-8 -*-

# Copyright 2010 István Szentandrási
# <etude-music-player@googlegroups.com>

# This file is part of Etude.
#
# Etude is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as 
# published by the Free Software Foundation, either version 3 
# of the License, or (at your option) any later version.
#
# Etude is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Etude. If not, see <http://www.gnu.org/licenses/>.

from . import _
import sys

try:
    import dbus, dbus.service
    if getattr(dbus, "version", (0, 0, 0)) >= (0, 41, 0):
        import dbus.glib
    if getattr(dbus, "version", (0, 0, 0)) >= (0, 80, 0):
        import _dbus_bindings as dbus_bindings
        NEW_DBUS = True
    else:
        import dbus.dbus_bindings as dbus_bindings
        NEW_DBUS = False
    HAVE_DBUS = True
except:
    HAVE_DBUS = False

def using_dbus():
    return HAVE_DBUS

def get_session_bus():
    try:
        return dbus.SessionBus()
    except Exception:
        print _("Etude failed to connect to DBUS session bus.")
        raise

def start_dbus_interface():
    if HAVE_DBUS:
        try:
            bus = get_session_bus()
            if NEW_DBUS:
                retval = bus.request_name("org.XMMS2.Etude",
                        dbus_bindings.NAME_FLAG_DO_NOT_QUEUE)
            else:
                retval = dbus_bindings.bus_request_name(bus.get_connection(), 
                        "org.XMMS2.Etude", dbus_bindings.NAME_FLAG_DO_NOT_QUEUE)
            if retval in (dbus_bindings.REQUEST_NAME_REPLY_PRIMARY_OWNER, 
                    dbus_bindings.REQUEST_NAME_REPLY_ALREADY_OWNER):
                pass
            elif retval in (dbus_bindings.REQUEST_NAME_REPLY_EXISTS, 
                    dbus_bindings.REQUEST_NAME_REPLY_IN_QUEUE):
                print _("An instance of Etude is already running. Showing it...")
                try:
                    obj = bus.get_object('org.XMMS2', '/org/XMMS2/Etude')
                    obj.show(dbus_interface='org.XMMS2.EtudeInterface')
                    sys.exit()
                except Exception:
                    print _("Failed to execute remote command.")
                    sys.exit(1)
        except Exception:
            pass
        except SystemExit:
            raise

if HAVE_DBUS:
    class EtudeDBus(dbus.service.Object):
        def __init__(self, dbus_show, dbus_toggle, dbus_popup):
            self.dbus_show = dbus_show
            self.dbus_toggle = dbus_toggle
            self.dbus_popup = dbus_popup
            session_bus = get_session_bus()
            bus_name = dbus.service.BusName('org.XMMS2', bus=session_bus)
            object_path = '/org/XMMS2/Etude'
            dbus.service.Object.__init__(self, bus_name, object_path)

        @dbus.service.method('org.XMMS2.EtudeInterface')
        def show(self):
            self.dbus_show()

        @dbus.service.method('org.XMMS2.EtudeInterface')
        def toggle(self):
            self.dbus_toggle()

        @dbus.service.method('org.XMMS2.EtudeInterface')
        def popup(self):
            self.dbus_popup()



